package interfaces;
import com.toedter.calendar.JDateChooser;

import dominio.Alquilerbicicleta;
import dominio.Alquilermaterialdeportivo;
import dominio.AlquilermaterialdeportivoId;
import dominio.Materialdeportivo;
import dominio.Socio;
import dominio.Usuario;
import excepciones.DAOExcepcion;
import gestion.AlquileresBicicleta;
import gestion.AlquileresMaterialDeportivo;
import gestion.MaterialesDeportivos;
import gestion.Socios;
import gestion.Usuarios;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class VentanaAlquilerMaterialDeportivo extends javax.swing.JDialog {
	private JLabel jLabelEsnCard;
	private JLabel jLabelFechaAlquiler;
	private JButton jButtonGuardar;
	private JButton jButtonCancelar;
	private JDateChooser jDateChooserFechaDevuelto;
	private JDateChooser jDateChooserFechaDevolucion;
	private JDateChooser jDateChooserFechaAlquiler;
	private JTextArea jTextAreaExtras;
	private JTextField jTextFieldIdMaterialDeportivo;
	private JTextField jTextFieldEsnCard;
	private JLabel jLabelExtras;
	private JLabel jLabelFechaDevuelto;
	private JLabel jLabelFechaDevolucion;
	private JLabel jLabelIdMaterialDeportivo;
	
	private Alquilermaterialdeportivo alquilerMaterialDeportivo = new Alquilermaterialdeportivo();
	private AlquileresMaterialDeportivo gestionAlquilerMaterialDeportivo = new AlquileresMaterialDeportivo();


	/**
	* Auto-generated main method to display this JDialog
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				JFrame frame = new JFrame();
				VentanaAlquilerMaterialDeportivo inst = new VentanaAlquilerMaterialDeportivo(frame);
				inst.setVisible(true);
			}
		});
	}
	
	public VentanaAlquilerMaterialDeportivo(JFrame frame) {
		super(frame);
		initGUI();
	}
	
	private void initGUI() {
		try {
			{
				GridBagLayout thisLayout = new GridBagLayout();
				thisLayout.columnWidths = new int[] {130, 10, 240};
				thisLayout.columnWeights = new double[] {0.0, 0.0, 0.0};
				thisLayout.rowHeights = new int[] {0, 0, 0, 0, 0, 40, 0, 40};
				thisLayout.rowWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
				getContentPane().setLayout(thisLayout);
				this.setTitle("ESN Office - Alquiler material deportivo");
				{
					jLabelEsnCard = new JLabel();
					getContentPane().add(jLabelEsnCard, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
					jLabelEsnCard.setText("ESN Card *");
				}
				{
					jLabelIdMaterialDeportivo = new JLabel();
					getContentPane().add(jLabelIdMaterialDeportivo, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
					jLabelIdMaterialDeportivo.setText("ID Material Deportivo *");
				}
				{
					jLabelFechaAlquiler = new JLabel();
					getContentPane().add(jLabelFechaAlquiler, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
					jLabelFechaAlquiler.setText("Fecha de alquiler *");
				}
				{
					jLabelFechaDevolucion = new JLabel();
					getContentPane().add(jLabelFechaDevolucion, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
					jLabelFechaDevolucion.setText("Fecha de devolución *");
				}
				{
					jLabelFechaDevuelto = new JLabel();
					getContentPane().add(jLabelFechaDevuelto, new GridBagConstraints(0, 6, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
					jLabelFechaDevuelto.setText("Fecha devuelto");
				}
				{
					jLabelExtras = new JLabel();
					getContentPane().add(jLabelExtras, new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
					jLabelExtras.setText("Extras");
				}
				{
					jTextFieldEsnCard = new JTextField();
					getContentPane().add(jTextFieldEsnCard, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
				}
				{
					jTextFieldIdMaterialDeportivo = new JTextField();
					getContentPane().add(jTextFieldIdMaterialDeportivo, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
				}
				{
					jTextAreaExtras = new JTextArea();
					getContentPane().add(jTextAreaExtras, new GridBagConstraints(2, 5, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
				}
				{
					jDateChooserFechaAlquiler = new JDateChooser("dd/MM/yyyy", "##-##-####", '-');
					getContentPane().add(jDateChooserFechaAlquiler, new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
				}
				{
					jDateChooserFechaDevolucion = new JDateChooser("dd/MM/yyyy", "##-##-####", '-');
					getContentPane().add(jDateChooserFechaDevolucion, new GridBagConstraints(2, 4, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
				}
				{
					jDateChooserFechaDevuelto = new JDateChooser("dd/MM/yyyy", "##-##-####", '-');
					getContentPane().add(jDateChooserFechaDevuelto, new GridBagConstraints(2, 6, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
				}
				{
					jButtonCancelar = new JButton();
					getContentPane().add(jButtonCancelar, new GridBagConstraints(2, 7, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
					jButtonCancelar.setText("Cancelar");
					jButtonCancelar.setPreferredSize(new java.awt.Dimension(90, 23));
					jButtonCancelar.setSize(70, 23);
					jButtonCancelar.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							jButtonCancelarActionPerformed(evt);
						}
					});
				}
				{
					jButtonGuardar = new JButton();
					getContentPane().add(jButtonGuardar, new GridBagConstraints(2, 7, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
					jButtonGuardar.setText("Guardar");
					jButtonGuardar.setPreferredSize(new java.awt.Dimension(90, 23));
					jButtonGuardar.setSize(70, 23);
					jButtonGuardar.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							try {
								jButtonGuardarActionPerformed(evt);
							} catch (DAOExcepcion e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					});
				}
			}
			this.setSize(480, 300);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void jButtonCancelarActionPerformed(ActionEvent evt) {
		System.out.println("jButtonCancelar.actionPerformed, event="+evt);
		this.dispose();
	}
	
	private void jButtonGuardarActionPerformed(ActionEvent evt) throws DAOExcepcion {
		System.out.println("jButtonGuardar.actionPerformed, event="+evt);
		//TODO add your code for jButtonGuardar.actionPerformed
		Alquilermaterialdeportivo alquilerMaterialDeportivo = new Alquilermaterialdeportivo();
		AlquileresMaterialDeportivo gestionAlquilerMaterialDeportivo = new AlquileresMaterialDeportivo();
		MaterialesDeportivos gestionMaterialDeportivo = new MaterialesDeportivos();
		
		String emailConnectado = "thalia.voukena@upv.esnvlc.es";
		Usuario usuario = new Usuario();
		Usuarios gestionUsuarios = new Usuarios();
		Socio socio = new Socio();
		Socios gestionSocios = new Socios();
		AlquilermaterialdeportivoId id = new AlquilermaterialdeportivoId();
		Materialdeportivo materialDeportivo = new Materialdeportivo();
		
		if (gestionSocios.comprobarSocio(jTextFieldEsnCard.getText())) {
			socio = gestionSocios.recuperarSocio(jTextFieldEsnCard.getText());
			alquilerMaterialDeportivo.setSocio(socio);
			int idMaterial = -1;
			if (jTextFieldIdMaterialDeportivo.getText().length() != 0) {
				try {
					idMaterial = Integer.parseInt(jTextFieldIdMaterialDeportivo.getText());
				}
				catch(NumberFormatException nFE) {
					JOptionPane error = new JOptionPane();
					error.showMessageDialog(null, "ID deberia ser un numero", "Error", JOptionPane.ERROR_MESSAGE);
				}
			}
			if (gestionMaterialDeportivo.comprobarMaterialDeportivoExiste(idMaterial)){
				materialDeportivo = gestionMaterialDeportivo.recuperarMaterialDeportivo(idMaterial);
				id.setIdmaterialdeportivo(materialDeportivo.getIdmaterialdeportivo());
				id.setEsncard(jTextFieldEsnCard.getText());
				alquilerMaterialDeportivo.setId(id);
				alquilerMaterialDeportivo.setFechaalquiler(jDateChooserFechaAlquiler.getDate());
				alquilerMaterialDeportivo.setFechadevolucion(jDateChooserFechaDevolucion.getDate());
				alquilerMaterialDeportivo.setExtras(jTextAreaExtras.getText());
				alquilerMaterialDeportivo.setFechadevuelto(jDateChooserFechaDevuelto.getDate());
				usuario = gestionUsuarios.recuperarUsuarioPorEmail(emailConnectado);
				alquilerMaterialDeportivo.setUsuario(usuario);
				
				if(gestionAlquilerMaterialDeportivo.comprobarAlquilerMaterialDeportivo(alquilerMaterialDeportivo)) {
					System.out.println(alquilerMaterialDeportivo.getFechaalquiler());
					if (gestionAlquilerMaterialDeportivo.anadirAlquilerMaterialDeportivo(alquilerMaterialDeportivo))
						this.dispose();
					else {
						JOptionPane error = new JOptionPane();
						error.showMessageDialog(null, "Fallo en guardado de datos", "Error", JOptionPane.ERROR_MESSAGE);
					}
				}
			}
			else {
				JOptionPane error = new JOptionPane();
				error.showMessageDialog(null, "No hay material deportivo con este id", "Error", JOptionPane.ERROR_MESSAGE);
			}
		}
		else {
			JOptionPane error = new JOptionPane();
			error.showMessageDialog(null, "No hay socio con esta ESN Card", "Error", JOptionPane.ERROR_MESSAGE);
		}
		
	}

}
